package tk.djcrazy.instantmessage.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import tk.djcrazy.instantmessage.domain.ChatMessage;
import tk.djcrazy.instantmessage.service.IChatMessageService;
import tk.djcrazy.instantmessage.service.IUserService;

import com.opensymphony.xwork2.ActionSupport;

public class MessageAction extends ActionSupport implements SessionAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5663615577986612816L;
	private String message;
	private int toUser;
 	private Map<String, Object> session;
 	private List<ChatMessage> chatMessages;
	
	private IChatMessageService chatMessageService;
	private IUserService userService;

	/**
	 * 
	 */

	@Override
	public String execute() {
		return INPUT;
	}

	public String sendMessage() {
		try {
		int fromUser = (Integer) session.get("uid");
		message = message.replaceAll("<", "{");
		message = message.replaceAll(">", "}");
 		chatMessageService.addMessage(fromUser,toUser, message);
		return SUCCESS; 
		}catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String getUserMessage() {
 		int userId =  (Integer) session.get("uid");
		chatMessages = chatMessageService.getMessages(userId);
 		for (ChatMessage chatMessage : chatMessages) {
			System.out.println(chatMessage.toString());
		}
 		return SUCCESS;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

 	public int getToUser() {
		return toUser;
	}

	public void setToUser(int toUser) {
		this.toUser = toUser;
	}

	public List<ChatMessage> getChatMessages() {
		return chatMessages;
	}

	public void setChatMessages(List<ChatMessage> chatMessages) {
		this.chatMessages = chatMessages;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public IChatMessageService getChatMessageService() {
		return chatMessageService;
	}

	public void setChatMessageService(IChatMessageService chatMessageService) {
		this.chatMessageService = chatMessageService;
	}

}
